package com.appspot.web2stanza.velocity;

import java.util.List;
import java.util.Properties;
import java.io.Writer;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.log.NullLogChute;

public class Builder {

  public static final String ENCODING = "UTF-8";

  private VelocityContext vc;

  public Builder(String key, Object obj) throws Exception {
    Properties p = new Properties();
    p.setProperty("input.encoding", ENCODING);
    p.setProperty("output.encoding", ENCODING);
    p.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
        NullLogChute.class.getName());
    Velocity.init(p);

    vc = new VelocityContext();

    // set object
    vc.put(key, obj);

    // set contributors
    /*
    LinkedList contributors = new LinkedList();

    Contributor test = new Contributor("ROLE", "VALUE");
    contributors.add(test);
    vc.put("contributors", contributors);
    */

  }

  public void mergeTemplate(String path, Writer writer) throws Exception {
    Velocity.mergeTemplate("/template/" + path + ".vm", ENCODING, vc, writer);
  }

}
